package util.concurrent;

import com.lidong.toolkit.utils.ThreadPoolUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 7/15/2022 4:25 PM
 */
public class AtomicIntegerTest {

    private static final Logger logger = LoggerFactory.getLogger(AtomicIntegerTest.class);

    @Test
    public void test1() throws InterruptedException {
        AtomicInteger num = new AtomicInteger(0);
        for (int i = 0; i < 5; i++) {
            ThreadPoolUtils.executor(() -> {
                for (int j = 0; j < 10; j++) {
                    dealMethodOne(num);
                }
            });
        }
        Thread.sleep(3000);
        logger.info(String.valueOf(num.get()));
    }

    private void dealMethodOne(AtomicInteger num) {
        int i = num.incrementAndGet();
        logger.info("Current thread {} i value {}", Thread.currentThread().getName(), i);
    }
}
